﻿using DtoModels;
using Entities;
using Microsoft.AspNetCore.Mvc;
using Portal.Models;
using SqlSugar;

namespace Portal.Controllers
{
    public class GoodsController : Controller
    {
        private readonly SqlSugarClient _sqlSugarClient;

        public GoodsController(SqlSugarClient sqlSugarClient)
        {
            _sqlSugarClient = sqlSugarClient;
        }

        public IActionResult LoadAllGoods(GoodsInfoReqDto goodsInfoReqDto)
        {
            var totalCount = 0;
            //var querySqlQueryable = this._sqlSugarClient.Context.Queryable<bus_goods>();

            var sql = $@"
select 
bg.*,bp.providerName
from bus_goods as bg
left join bus_provider as bp on bg.providerId = bp.id
";

            var querySqlQueryable = this._sqlSugarClient.SqlQueryable<BusGoodsViewModel>(sql);

            var queryResult = querySqlQueryable.ToPageList(goodsInfoReqDto.Page, goodsInfoReqDto.Limit, ref totalCount);

            var result = new
            {
                Code = 0,
                Msg = "查询成功",
                Count = totalCount,
                Data = queryResult
            };

            return Json(result);
        }

        public IActionResult LoadAllGoodsForSelect()
        {
            var queryResult = this._sqlSugarClient.Context.Queryable<bus_goods>().ToList();
            var result = new
            {
                Code = 0,
                Msg = "查询成功",
                Count = queryResult.Count,
                Data = queryResult
            };

            return Json(result);
        }

        /// <summary>
        /// 更新产品
        /// </summary>
        /// <returns></returns>
        public IActionResult UpdateGoods(bus_goods busGoods)
        {
            var updateNum = this._sqlSugarClient.Updateable<bus_goods>(busGoods).ExecuteCommand();

            var result = new LayUiStandardResDto<object>
            {
                Code = GlobalVariable.Ok,
                Msg = $"成功更新了{updateNum}个产品信息"
            };

            return Json(result);
        }

        /// <summary>
        /// 根据供应商查询商品
        /// </summary>
        /// <param name="providerId"></param>
        /// <returns></returns>
        public IActionResult LoadGoodsByProviderId(int providerId)
        { 
            var queryResult = this._sqlSugarClient.Queryable<bus_goods>().Where(x => x.providerid == providerId).ToList();

            var result = new
            {
                Code = 0,
                Msg = "查询成功",
                Count = queryResult.Count,
                Data = queryResult
            };

            return Json(result);
        }

        /// <summary>
        /// 删除商品
        /// </summary>
        /// <param name="id">商品信息Id</param>
        /// <param name="goodsimg">商品图片路径</param>
        /// <returns></returns>
        public IActionResult DeleteGoods(int id, string goodsimg)
        { 
            var deleteNum = this._sqlSugarClient.Deleteable<bus_goods>().Where(x => x.id == id).ExecuteCommand();

            var result = new LayUiStandardResDto<object>
            {
                Code = GlobalVariable.Ok,
                Msg = $"成功删除了{deleteNum}个产品信息"
            };

            return Json(result);
        }
    }
}